package com.huike.web.controller;

import com.github.pagehelper.PageInfo;
import com.huike.entity.Department;
import com.huike.query.QueryObject;
import com.huike.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/list")
    public ModelAndView getDepartmentList(@ModelAttribute("qo") QueryObject qo, ModelAndView mav){
        PageInfo<Department> page = departmentService.getPage(qo.getCurrentPage(), qo.getPageSize());
//        System.out.println(page.getList());
//        System.out.println(page.getPageSize());
//        mav.addObject("qo",qo);
        mav.addObject("pageInfo",page);
        mav.setViewName("department/list");
        return mav ;
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"部门编辑","department:input"},logical = Logical.OR)
    public String addDept(Department department){
        departmentService.saveOrUpdate(department);
        return "redirect:/department/list";
    }


    @RequestMapping("/delete")
    public String deleteDept(Integer id){
        departmentService.deleteDept(id);
        return "redirect:/department/list";
    }
}
